<template>
	<view>
		<view class="detail-container">
			<!-- <image :src="detailData.img1" mode="widthFix" style="width: 100%;" v-if="detailData.img1"/> -->
				<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" style="width: 100%; height: 200px;">
						<swiper-item style="width: 100%; height: 100%;">
							<image :src="detailData.img1" style="width: 100%; height: 100%; object-fit: cover;" v-if="detailData.img1"/>
						</swiper-item>
						<swiper-item style="width: 100%; height: 100%;">
							<image :src="detailData.img2" style="width: 100%; height: 100%; object-fit: cover;" v-if="detailData.img2"/>
						</swiper-item>
						<swiper-item style="width: 100%; height: 100%;">
							<image :src="detailData.img3" style="width: 100%; height: 100%; object-fit: cover;" v-if="detailData.img3"/>
						</swiper-item>
				</swiper>
			<h2 class="detail-title">{{ detailData.proname || '加载中...' }}</h2>
				商品价格: <span class="price-num">{{ detailData.originprice || 0 }}</span> 元/件
				<br />
				{{ detailData.desc  }}
				<br/>
				<h3 class="count-label">数量：</h3>
				<button size="mini" @click="delfn">-</button>{{count}}<button size="mini" @click="addfn">+</button>
			</view>
			<button type="warn" @click="sendfn">加入购物车</button>
			<button type="primary" @click="gocart">去购物车</button>
		</view>
	</template>

<script>
	export default {
		data() {
			return {
				detailData: {}, 
				count: 1,      
				proid: '',
			}
		},
		onLoad(options) {
			this.proid = options.proid;
			this.loadData();
		},
		methods: {
			loadData() {
				uni.request({
					url: `http://47.94.6.186:3001/api/pro/detail/${this.proid}`,
					method: 'GET',
					success: res => {
						console.log('详情数据:', res.data);
						this.detailData = res.data.data || {};
					},
					fail: () => {
						uni.showToast({ title: '详情加载失败', icon: 'none' });
					}
				});
			},
			delfn(){
				if(this.count<=1){
					return
				}else{
					this.count--
				}
			},
			addfn(){
				this.count++
			},
			sendfn(){
				const token = uni.getStorageSync('token');
				const userid = uni.getStorageSync('userid'); 
				    if (!token || !userid) {
				        uni.showToast({ title: '请先登录', icon: 'none' });
				        return;
				      }
				      uni.request({
				        url: 'http://47.94.6.186:3001/api/cart/add',
				        method: 'POST',
				        header: {'token': token  },
				        data: {
				          userid: userid, 
				          proid: this.proid, 
				          num: this.count  
				        },
				        success: res => {
				          console.log(res.data);
				            uni.showToast({ title: '加入购物车成功', icon: 'none' });
				        },
				      });
				    },
			gocart(){
				wx.switchTab({ url: '/pages/procart/procart' });
			},
		}
	}
</script>

<style scoped>
</style>